package com.ruoyi.produce.base.bom.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.base.bom.domain.Bom;
import com.ruoyi.produce.base.bom.service.IBomService;
import com.ruoyi.psi.base.product.domain.ProductCond;
import com.ruoyi.psi.base.product.domain.ProductDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Bom信息
 * @author Drj 2021/6/28
 */
@RestController
@RequestMapping("/produce/bom")
@Api(value = "Bom信息模块", tags = "Bom信息接口")
public class BomController extends BaseController {
    @Autowired
    private IBomService bomService;

    /**
     * 获取存在Bom的所有产品
     * @return
     */
    @PostMapping("/selectProduce")
    public TableDataInfo selectProduce(@RequestBody ProductCond cond){
        startPage();
        List<ProductDto> productDtos = bomService.selectProduceForBom(cond);
        return getDataTable(productDtos);
    }
    /**
     * 获取Bom
     * @param bom
     * @return
     */
    @ApiOperation(value = "获取Bom列表",notes = "查询所有Bom信息")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody Bom bom){
        startPage();
        if (StringUtils.isNull(bom.getProduceId())){
            //第一次或者为输入产品编号
            return AjaxResult.success("请选择产品编号！");
        }
        if (StringUtils.isNotNull( bomService.selectBom(bom.getProduceId()))){
            Bom selectBom = bomService.selectBom(bom.getProduceId());
//            System.out.println(selectBom);
            return AjaxResult.success(selectBom);
        }else{
            return AjaxResult.error("该产品没有对应的产品结构清单！请重新选择。");
        }
    }



    /**
     * 新增Bom
     * @param bom
     * @return
     */
    @ApiOperation(value = "新增Bom", notes = "新增Bom")
    @ApiImplicitParam(name = "bom", value = "新增Bom", dataType = "Bom")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Bom bom) throws Exception {
        if (StringUtils.isNull(bom.getProduceChild())){
            return AjaxResult.error("未选择产品子件！");
        }
        if(bomService.insertBom(bom) == 0){
            return AjaxResult.error("该产品已存在对应产品结构清单！");
        }else{
            return AjaxResult.success("新增成功");
        }
    }

    /**
     * 修改Bom保存
     * @param bom
     * @return
     */
    @ApiOperation(value = "修改Bom", notes = "修改Bom")
    @ApiImplicitParam(name = "bom", value = "修改Bom", dataType = "Bom")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Bom bom) throws Exception {
        if (StringUtils.isNull(bom.getProduceChild())){
            return AjaxResult.error("未选择产品子件！");
        }
        if(bomService.updateBom(bom) == 0){
            return AjaxResult.error("该产品不存在对应产品结构清单！");
        }else{
            return AjaxResult.success("修改成功");
        }
    }

    /**
     * 删除Bom
     * 仅修改删除标志位
     * @param bom
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Bom bom)
    {
        return toAjax(bomService.updateBomDeleteFlag(bom));
    }

    /**
     * 新增时查验是否已存在对应产品结构清单
     * 选中父件产品时便检验
     * @param bom
     * @return
     */
    @PostMapping("/checkBomAdd")
    public AjaxResult checkBomAdd(@RequestBody Bom bom){
        if (!bomService.checkBomExist(bom)){
            return AjaxResult.error("已建立了该产品的物资材料清单，不能再新增");
        }
        return AjaxResult.success();
    }

    @PostMapping("/checkBomRelationship")
    public AjaxResult checkBomRelationship(@RequestBody Bom bom){
        if (bomService.checkBomParent(bom)){
            return AjaxResult.success("已添加子件");
        }
        return AjaxResult.error("存在子父构件循环，不能添加");
    }

    /**
     * 获取工序
     * @param bom
     * @return
     */
    @PostMapping("/getProcedureById")
    public AjaxResult getProcedureById(@RequestBody Bom bom){
        return AjaxResult.success( bomService.selectProcedureList(bom.getProduceId()));
    }

    /**
     * 获取设备
     * @param bom
     * @return
     */
    @PostMapping("/getMachineById")
    public AjaxResult getMachineById(@RequestBody Bom bom){
        return AjaxResult.success( bomService.selectMachineList(bom.getProduceId()));
    }

    /**
     * 获取模具
     * @param bom
     * @return
     */
    @PostMapping("/getMouldById")
    public AjaxResult getMouldById(@RequestBody Bom bom){
        return AjaxResult.success( bomService.selectMouldList(bom.getProduceId()));
    }

    /**
     * 删除父件对应的指定子件
     * @return
     */
    @GetMapping("/delete/{produceId}{produceParentId}")
    public AjaxResult deleteBomChild(@PathVariable String produceId, @PathVariable String produceParentId)
    {
        return toAjax(bomService.deleteBomChild(produceId, produceParentId));
    }
}
